package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.INodeService;
import com.mdd.admin.validate.NodeCreateValidate;
import com.mdd.admin.validate.NodeUpdateValidate;
import com.mdd.admin.validate.NodeSearchValidate;
import com.mdd.admin.vo.NodeListedVo;
import com.mdd.admin.vo.NodeDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.Node;
import com.mdd.common.mapper.NodeMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 节点实现类
 * @author LikeAdmin
 */
@Service
public class NodeServiceImpl implements INodeService {
        
    @Resource
    NodeMapper nodeMapper;

    /**
     * 节点列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<NodeListedVo>
     */
    @Override
    public PageResult<NodeListedVo> list(PageValidate pageValidate, NodeSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<Node> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        nodeMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:nodeName@node_name:str",
            "=:personnelType@personnel_type:str",
            "=:isEdit@is_edit:str",
            "=:nextStep@next_step:int",
            "=:number:int",
            "=:flowNodeId@flow_node_id:int",
            "=:userId@user_id:int",
            "like:userName@user_name:str",
            "=:updateUserId@update_user_id:int",
            "like:updateUserName@update_user_name:str",
        });

        IPage<Node> iPage = nodeMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<NodeListedVo> list = new LinkedList<>();
        for(Node item : iPage.getRecords()) {
            NodeListedVo vo = new NodeListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 节点详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return Node
     */
    @Override
    public NodeDetailVo detail(Integer id) {
        Node model = nodeMapper.selectOne(
                new QueryWrapper<Node>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        NodeDetailVo vo = new NodeDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 节点新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(NodeCreateValidate createValidate) {
        Node model = new Node();
        model.setNodeName(createValidate.getNodeName());
        model.setPersonnelType(createValidate.getPersonnelType());
        model.setIsEdit(createValidate.getIsEdit());
        model.setNextStep(createValidate.getNextStep());
        model.setNumber(createValidate.getNumber());
        model.setFlowNodeId(createValidate.getFlowNodeId());
        model.setUserId(createValidate.getUserId());
        model.setUserName(createValidate.getUserName());
        model.setUpdateUserId(createValidate.getUpdateUserId());
        model.setUpdateUserName(createValidate.getUpdateUserName());
        nodeMapper.insert(model);
    }

    /**
     * 节点编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(NodeUpdateValidate updateValidate) {
        Node model = nodeMapper.selectOne(
                new QueryWrapper<Node>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setNodeName(updateValidate.getNodeName());
        model.setPersonnelType(updateValidate.getPersonnelType());
        model.setIsEdit(updateValidate.getIsEdit());
        model.setNextStep(updateValidate.getNextStep());
        model.setNumber(updateValidate.getNumber());
        model.setFlowNodeId(updateValidate.getFlowNodeId());
        model.setUserId(updateValidate.getUserId());
        model.setUserName(updateValidate.getUserName());
        model.setUpdateUserId(updateValidate.getUpdateUserId());
        model.setUpdateUserName(updateValidate.getUpdateUserName());
        nodeMapper.updateById(model);
    }

    /**
     * 节点删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        Node model = nodeMapper.selectOne(
                new QueryWrapper<Node>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(String.valueOf(System.currentTimeMillis() / 1000));
        nodeMapper.updateById(model);
    }

}
